package com.mzy.designMode.adapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2022/5/16 22:59
 * @Version 1.0
 */
public class ObserverPattern {
    public static void main(String[] args) {
        zhangSan zhangSan = new zhangSan();
        zhangSan.borrow(new wangWu());
        zhangSan.borrow(new zhaoSi());
        // state改变 if()
        zhangSan.notifyCredits();
    }
}

// 借贷
interface Debit{
    // 借钱
    void borrow(Credit credit);
    // 通知机制
    void notifyCredits();
}

// 贷款方
interface Credit{
    void takeMoney();
}

class zhangSan implements Debit{

    // 所有的贷款方
    private List<Credit> allCredits = new ArrayList<>();
    private Integer state = 0; // 1表示有钱

    @Override
    public void borrow(Credit credit) {
        allCredits.add(credit);
    }

    @Override
    public void notifyCredits() {
        allCredits.forEach(credit -> credit.takeMoney());
    }
}

class wangWu implements Credit{

    @Override
    public void takeMoney() {
        System.out.println("王五还钱");
    }
}

class zhaoSi implements Credit{

    @Override
    public void takeMoney() {
        System.out.println("赵四还钱");
    }
}